;
(function() {
	module.exports = {
		//增加订单
		addAch: function(ach_id) {
			var self = this;
			self.ach.unshift(ach_id);
			db.update(config.dbname.bus, self._id, self);
		},
		//代码片段，获取ach对象
		_getAch: function(ach_id) {
			var achs = this.ach || [];
			if (achs.indexOf(ach_id) === -1) {
				throwCusError("订单不属于指定商家")
			}
			var ach = AchCon.get(ach_id);
			if (!ach) {
				throwCusError("找不到指定订单");
			}
			return ach;
		},
		//不被显示的订单
		_getHiddenAchs: function() {
			var hidden_achs = this.hidden_achs || (this.hidden_achs = []);
			return hidden_achs;
		},
		//获取订单信息
		getAchs: function(num, page, filter) {
			var self = this;
			var achs = self.ach || [];
			var result = [];
			if (filter) {
				achs.forEach(function(ach_id) {
					var ach = AchCon.get(ach_id)
					if (ach && filter(ach)) {
						result.push(ach);
					}
				});
				result = data_cut.page(result, num, page)
			} else {
				achs = data_cut.page(achs, num, page);
				achs.forEach(function(ach_id) {
					var ach = AchCon.get(ach_id)
					if (ach) {
						result.push(ach);
					}
				});
			}
			return result;
		},
		getAchsWithType: function(type, num, page) {
			if (type === "未付款") {
				var achs = this.getAchs(num, page, function(ach) {
					//前者等待用户付款后确认收款，这个期间默认为未付款
					return ach.info.state == AchCon.STATE.确认收获并付款 || ach.info.state == AchCon.STATE.未付款
				});
			} else if (type === "未发货") {
				var achs = this.getAchs(num, page, function(ach) {
					return ach.info.state == AchCon.STATE.未发货 || ach.info.state == AchCon.STATE.已付款
				});
			} else {
				var achs = this.getAchs(num, page);
			}
			return achs;
		},
		getAchsWithType_PageInfo: function(type, num, page) {
			var self = this;
			var achs = this.getAchsWithType(type);
			var total_num = achs.length;
			//分页
			achs = data_cut.page(achs, num, page);
			//加上用户信息
			achs = achs.map(function(ach) {
				return ach.getDetail();
			});
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				achs: achs,
				page: +page || 0,
				num: +num || 0
			}
		},
		getAchsDetail: function(num, page) {
			var self = this;
			var achs = this.getAchs(num, page);
			//包括用户信息
			achs = achs.map(function(ach) {
				return ach.getDetail();
			});
			return achs;
		},
		getAchsTotalNum: function() {
			return this.achs.length;
		},
		getAchsDetail_PageInfo: function(num, page) {
			var achs = this.getAchsDetail(num, page);
			var total_num = this.getAchsTotalNum();
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				achs: achs,
				page: +page || 0,
				num: +num || 0
			}
		},
		setAchExpressId: function(ach_id, express_id) {
			var ach = this._getAch(ach_id);
			ach._setExpressId(express_id);
			return ach_id;
		},
		/*在线支付*/
		//非安全，第三方付款工具的回调核心
		_state_ach_ol未付款_to_ol未发货: function(ach_id) {
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.ol未付款) {
				var result = ach._nextFlowStatus();
				ach.setInfo({
					pay_time: +new Date
				});
				console.log("触发在线支付完成交易的回调");
				ach.emitBuffs(BuffCon.env.afterPayAch);
				return result;
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		state_ach_ol未发货_to_ol未收货: function(info) {
			var ach_id = info.ach_id;
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.ol未发货) {
				this.ach_set_ach_express(ach_id, info.express_info)
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		/*货到付款*/
		state_ach_未发货_to_已发货: function(info) {
			var ach_id = info.ach_id;
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.未发货) {
				this.ach_set_ach_express(ach_id, info.express_info)
				return ach._nextFlowStatus();
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		state_ach_未收货未付款_to_交易完成: function(ach_id) {
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.未收货未付款) {
				console.log("触发货到付款完成交易的回调");
				ach.emitBuffs(BuffCon.env.afterPayAch);
				var result = ach._nextFlowStatus();
				return result;
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		/*当面付款*/
		state_ach_face未付款_to_face交易完成: function(ach_id) {
			var ach = this._getAch(ach_id);
			if (ach.info.state == AchCon.STATE.face未付款) {
				console.log("触发货到付款完成交易的回调");
				ach.emitBuffs(BuffCon.env.afterPayAch);
				var result = ach._nextFlowStatus();
				return result;
			} else {
				throwCusError("订单所在状态无法进行相应转换")
			}
		},
		/*通用API*/
		//商家自定义订单价格
		ach_change_cash: function(ach_id, cus_cash) {
			var ach = this._getAch(ach_id);
			ach._customCash(cus_cash);
			return ach;
		},
		//商家设置快递信息（公司、快递单号）
		ach_set_ach_express: function(ach_id, express_info) {
			var ach = this._getAch(ach_id);
			ach._setAchExpress(express_info);
			return ach;
		},
		//非安全，收到用户确认的订单
		//推送消息给商家
		_receiveConfirmAch: function(ach_id) {
			var self = this;
			self.addAch(ach_id);
			console.log("商家收到订单“", ach_id, "”！");
		},
		//解锁（退回）订单，使得用户有权限修改订单
		unlockAch: function(ach_id) {
			var ach = AchCon.get(ach_id);
			ach.unlock();
			console.log("商家解锁订单“", ach_id, "”！");
		},
		//代码片段，从商家订单列表中移除订单
		_removeAch: function(ach_id) {
			var achs = (this.ach || (this.ach = []));
			var result = $Array.remove(achs, ach_id);
			if (result.length !== achs.length) {
				this.ach = result;
				this.save();
				return result;
			}
			return false;
		},
		hiddenAch: function(ach_id) {
			var achs = this.ach || [];
			var index = achs.indexOf(ach_id);
			if (index === -1) {
				throwCusError("订单不属于指定商家")
			}
			this._getHiddenAchs().push(ach_id);
			achs.splice(index, 1);
			this.save();
		},
		// /*遗弃的API*/
		// //订单配置的修改
		// getAchPoints: function(ach_id) {
		// 	var ach = this._getAch(ach_id);
		// 	return ach.getAchPoints();
		// },
		// updateAchPoints: function(ach_id, ach_points) {
		// 	var ach = this._getAch(ach_id);
		// 	ach._updateAchPoints(ach_points);
		// 	return ach.getAchPoints();
		// },
		//查询订单
		findAchs: function(num, page, ach_key) {
			ach_key = ach_key.split(/[\,\s\，]+/);
			ach_key = $Array.clearNull(ach_key);
			console.log(ach_key);
			if (ach_key.length === 0) {
				throwCusError("搜索的关键字不可为空");
			} else if (ach_key.length === 1) {
				ach_key = ach_key[0];
				var filter_achs = this.getAchs(num, page, function(ach) {
					return ach._id.indexOf(ach_key) !== -1
				});
			} else {
				filter_achs = this.getAchs(num, page, function(ach) {
					var _id = ach._id;
					//是否完全不匹配
					var no_match = ach_key.every(function(key) {
						return _id.indexOf(key) === -1
					});
					return !no_match;
				});
			}
			return filter_achs;
		}
	};
	var db = require("../db/index");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());